/**
 * @author zhounan 
 * @date 2024年6月20日
 * @version V1.0
 */
package org.zn.etl.controller;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.zn.etl.config.Constant;
import org.zn.etl.datasource.DataSourceFactoryHolder;
import org.zn.etl.job.context.ClusterJobExecInfo;
import org.zn.etl.job.context.JobExecInstanceContext;
import org.zn.etl.job.context.NodeRunningContextManager;

import com.zaxxer.hikari.HikariDataSource;

/**
 * @author zhounan 
 * @date 2024年6月20日
 * @version V1.0
 */
@RequestMapping("/etl/runtime")
@RestController
public class EtlRunTimeController {
	
	@Autowired
	private NodeRunningContextManager nodeManger;
	@Autowired
	private DiscoveryClient discoveryClient;
	
	@Autowired
	private	RestTemplate restTemplate;
	
	@Autowired
	private DataSourceFactoryHolder factoryHolder; 
	
	/**
	 * 集群任务执行列表
	 * @return
	 */
	@GetMapping("cluster/list")
	public List<?> clusterList(){
		return nodeManger.getClusterList();
		
	}
	
	/**
	 * 节点任务执行列表
	 * @return
	 */
	@GetMapping("node/list")
	public List<?> nodeList(){
		return nodeManger.getNodeList();
		
	}
	
	/**
	 * 查询集群任务执行信息
	 * @param name
	 * @return
	 */
	@GetMapping("cluster/job")
	public  ClusterJobExecInfo clusterJob(String name){
		return nodeManger.findCluster(name);
	}
	
	
	/**
	 * 删除任务信息
	 * @param name
	 * @return
	 */
	@DeleteMapping("job/debug")
	public boolean removeCluster(String name){
		
		return nodeManger.renmoveJobExecIns(name);
		
	}
	

	/**
	 * 服务节点信息
	 * @param serviceName
	 * @return
	 */
	@GetMapping("service/list")
	public List<?> serviceList(String serviceName){
	
		List<ServiceInstance>  slist=discoveryClient.getInstances(serviceName);
		
		return slist;
		
	}
	
	/**
	 * 取消执行任务
	 * @param name
	 * @param broadcast
	 * @return
	 */
	@DeleteMapping("job")
	public Boolean cancelJob(String name,Boolean broadcast){
		
		JobExecInstanceContext jei=nodeManger.findNode(name);
		
		Boolean  res=false;

		if(jei!=null) {
			
			nodeManger.cancel(name);
			nodeManger.renmoveJobExecIns(name);
			res=true;
			
		}else {
			if(! Boolean.FALSE.equals(broadcast)) {
				
				List<ServiceInstance>  slist=discoveryClient.getInstances(Constant.SERVICE_NAME);
				
				for (ServiceInstance si : slist) {
					
					si.getHost().equals(nodeManger.getHostAddress());
					
					String path=si.getUri()+"/etl/runtime/job?name="+name+"&broadcast=false";
					
					res=restTemplate.postForEntity(path, null, Boolean.class).getBody();
					
					if(res)  break;
					
				}
			}
		}
				
		return res;
		
	}
	
	
	
	
	@GetMapping("/dataSource/hikari")
	public  Object dataSourceStatus(String dataSourceKey) {
		
		DataSource ds=factoryHolder.find(dataSourceKey);
		
		HikariDataSource hds=  (HikariDataSource) ds;
		
		return hds;
		
		
	}


}
